package com.example.log.impl;

import com.example.log.exception.ServiceException;
import com.example.log.vo.LogVO;
import com.example.log.vo.PageVO;
import com.example.log.mapper.LogMapper;
import com.example.log.pojo.Log;
import com.example.log.service.LogService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;
import tk.mybatis.mapper.entity.Example;

import java.util.ArrayList;
import java.util.List;

/**
 * 〈操作日志管理〉<br>
 * 〈〉
 * @author  SongPeng
 * @date  2021/11/11 21:24
 * @version 1.0
 */

@Service
public class LogServiceImpl implements LogService {
    @Autowired
    private LogMapper logMapper;

   @Override
    public void deleteById(Long operateId) {
        logMapper.deleteByPrimaryKey(operateId);
    }

    /**
     * 日志列表
     * */

    @Override
    public PageVO<LogVO> findLogList(Integer pageNum, Integer pageSize, LogVO logVO) {
        PageHelper.startPage(pageNum,pageSize);
        Example o = new Example(Log.class);
        Example.Criteria criteria = o.createCriteria();
        //降序排序
        o.setOrderByClause("operate_actiondate desc");
        if(logVO.getOperateActionclassname()!=null&&!"".equals(logVO.getOperateActionclassname())){
            criteria.andLike("operate_actionclassname","%"+logVO.getOperateActionclassname()+"%");
        }
        if(logVO.getOperateLoggername()!=null&&!"".equals(logVO.getOperateLoggername())){
            criteria.andLike("operate_loggername","%"+logVO.getOperateLoggername()+"%");
        }
        List<Log> Logs = logMapper.selectByExample(o);
        List<LogVO> logVOS=new ArrayList<>();
        if(!CollectionUtils.isEmpty(Logs)){
            for (Log loginLog : Logs) {
                LogVO logVO1 = new LogVO();
                BeanUtils.copyProperties(loginLog,logVO1);
                logVOS.add(logVO1);
            }
        }
        PageInfo<Log> info=new PageInfo<>(Logs);
        return new PageVO<>(info.getTotal(),logVOS);
    }

    @Override
    public List<Log> findAll() {
        return logMapper.selectAll();
    }
    /**
     * 批量删除
     * @param list
     */
    @Override
    public void batchDelete(List<Long> list) {
        for (Long id : list) {
            Log log = logMapper.selectByPrimaryKey(id);
            if(log==null){
                throw new ServiceException("id="+id+",操作日志不存在");
            }
            deleteById(id);
        }
    }
}
